1
|
|
|
import fs from 'fs'; |
2
|
|
|
import path from 'path'; |
3
|
|
|
import { all, promisify } from 'bluebird'; |
4
|
|
|
import { compile } from 'handlebars'; |
5
|
|
|
import { |
6
|
|
|
keys, |
7
|
|
|
merge, |
8
|
|
|
replace, |
9
|
|
|
values, |
10
|
|
|
zipWith |
11
|
|
|
} from 'ramda'; |
12
|
|
|
import { version as rungCliVersion } from '../package'; |
13
|
|
|
import { getProperties } from './vm'; |
14
|
|
|
import { readFile, compileSources } from './run'; |
15
|
|
|
import { getTypeName } from './types'; |
16
|
|
|
import { compileES6 } from './compiler'; |
17
|
|
|
import { emitSuccess } from './input'; |
18
|
|
|
|
19
|
|
|
const createFile = promisify(fs.writeFile); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns the source Handlebars template as string |
23
|
|
|
* |
24
|
|
|
* @return {Promise} |
25
|
|
|
*/ |
26
|
|
|
function getHandlebarsTemplate() { |
27
|
|
|
return readFile(path.join(__dirname, '../templates/readme.hb'), 'utf-8') |
28
|
|
|
.then(compile); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Converts an object to an array containing a list of { name, version } |
33
|
|
|
* |
34
|
|
|
* @param {Object} dependencies |
35
|
|
|
* @return {Array} |
36
|
|
|
*/ |
37
|
|
|
function dependenciesToArray(dependencies) { |
38
|
|
|
return zipWith((name, version) => ({ name, version }), |
39
|
|
|
keys(dependencies), |
40
|
|
|
values(dependencies)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Converts an object to an array containing a list of { name, type, description } |
45
|
|
|
* |
46
|
|
|
* @param {Object} parameters |
47
|
|
|
* @return {Array} |
48
|
|
|
*/ |
49
|
|
|
function parametersToArray(parameters) { |
50
|
|
|
return zipWith( |
51
|
|
|
(name, { type, description }) => ({ |
52
|
|
|
name, |
53
|
|
|
type: getTypeName(type), |
54
|
|
|
description }), |
55
|
|
|
keys(parameters), |
56
|
|
|
values(parameters)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Generates a full README in Markdown with documentation about input parameters |
61
|
|
|
* and the business rules of the app |
62
|
|
|
* |
63
|
|
|
* @return {Promise} |
64
|
|
|
*/ |
65
|
|
|
export default function readme() { |
66
|
|
|
return readFile('package.json', 'utf-8') |
67
|
|
|
.then(JSON.parse) |
68
|
|
|
.then(({ name, version, author, dependencies }) => |
69
|
|
|
all([{ |
70
|
|
|
rungCliVersion, |
71
|
|
|
name, |
72
|
|
|
version, |
73
|
|
|
author, |
74
|
|
|
escapedName: replace(/-/g, '--', name), |
75
|
|
|
dependencies: dependenciesToArray(dependencies) }, |
76
|
|
|
compileSources() |
77
|
|
|
.spread((source, modules) => |
78
|
|
|
getProperties({ name: 'pre-compile', source: compileES6(source) }, {}, modules))])) |
79
|
|
|
.spread((partialContext, result) => merge(partialContext, { |
80
|
|
|
parameters: parametersToArray(result.params), |
81
|
|
|
description: result.description, |
82
|
|
|
title: result.title |
83
|
|
|
})) |
84
|
|
|
.then(context => all([context, getHandlebarsTemplate()])) |
85
|
|
|
.spread((context, generateReadme) => generateReadme(context)) |
86
|
|
|
.then(createFile('README.md', _)) |
|
|
|
|
87
|
|
|
.tap(~emitSuccess('generated README.md')); |
88
|
|
|
} |
89
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.